import json
import leancloud
import os
import datetime
leancloud.init("FuEKBAKt7fr6CnVTfMVXKyMm-gzGzoHsz", "9Gw1lGPn1fgxJ3gKw04BltIU")
area = leancloud.Object.extend('Area')
area_data = leancloud.Object.extend('AreaData')
for file in os.listdir("../dataset/to_upload"):
    print(file)
    with open("../dataset/to_upload/" + file) as f:
        area_save_list = list()
        area_data_save_list = list()
        date = datetime.datetime(2020,
                                 int(file[:-4][0:2]),
                                 int(file[:-4][2:4]),
                                 int(file[:-4][4:6]),
                                 int(file[:-4][6:8]),
                                 int(file[:-4][8:10]))
        rawdata = json.loads(f.read())
        for data in rawdata:
            if data["provinceName"] != "中国":
                for t in ("confirmedCount", "curedCount", "deadCount"):
                    ad = area_data()
                    ad.set("event_name", "2019-nCoV")
                    ad.set("area_name", data["provinceName"])
                    ad.set("time", date)
                    ad.set("type", t)
                    ad.set("value", data[t])
                    area_data_save_list.append(ad)
            '''
            query = area.query                                                 #Area对象先查找若没有则创建
            query.equal_to('event_name', '2019-nCoV')
            query.equal_to('name', data["provinceName"])
            area_list = query.find()
            if area_list == []:
                country = area()
                country.set("event_name", "2019-nCoV")
                country.set("name", data["provinceName"])
                country.set("parent_area_name", "")
                area_save_list.append(country)
            else:
                country = area_list[0]
            for t in ("confirmedCount", "suspectedCount", "curedCount", "deadCount", "seriousCount"):
                query = area_data.query
                query.equal_to('event_name', '2019-nCoV')
                query.equal_to("area_name", data["provinceName"])
                query.equal_to("time", date)
                query.equal_to("type", t)
                area_data_list = query.find()
                if area_data_list == []:
                    ad = area_data()
                    ad.set("event_name", "2019-nCoV")
                    ad.set("area_name", data["provinceName"])
                    ad.set("time", date)
                    ad.set("type", t)
                else:
                    ad = area_data_list[0]
                ad.set("value", data[t])
                area_data_save_list.append(ad)
            for pdata in data['cities']:
                query = area.query
                query.equal_to('event_name', '2019-nCoV')
                query.equal_to('name', pdata["provinceShortName"])                       #AreaData对象先查找若没有则创建，若有则更新
                area_list = query.find()
                if area_list == []:
                    p = area()
                    p.set("event_name", "2019-nCoV")
                    p.set("name", pdata["provinceShortName"])
                    p.set("parent_area_name", data["provinceName"])
                    area_save_list.append(p)
                else:
                    p = area_list[0]
                for t in ("confirmedCount", "curedCount", "deadCount"):
                    query = area_data.query
                    query.equal_to('event_name', '2019-nCoV')
                    query.equal_to("area_name", pdata["provinceShortName"])
                    query.equal_to("time", date)
                    query.equal_to("type", t)
                    area_data_list = query.find()
                    if area_data_list == []:
                        ad = area_data()
                        ad.set("event_name", "2019-nCoV")
                        ad.set("area_name", pdata["provinceShortName"])
                        ad.set("time", date)
                        ad.set("type", t)
                    else: 
                        ad = area_data_list[0]
                    ad.set("value", pdata[t])
                    area_data_save_list.append(ad)
                for cdata in pdata["cities"]:
                    if "外地" not in cdata["cityName"] and "明确" not in cdata["cityName"]:
                        if cdata["cityName"] == "吉林":
                            cdata["cityName"] += "市"
                        query = area.query
                        query.equal_to('event_name', '2019-nCoV')
                        query.equal_to('name', cdata["cityName"])
                        area_list = query.find()
                        if area_list == []:
                            c = area()
                            c.set("event_name", "2019-nCoV")
                            c.set("name", cdata["cityName"])
                            c.set("parent_area_name", pdata["provinceShortName"])
                            area_save_list.append(c)
                        else:
                            c = area_list[0]
                        for t in ("confirmedCount", "curedCount", "deadCount"):
                            query = area_data.query
                            query.equal_to('event_name', '2019-nCoV')
                            query.equal_to("area_name", cdata["cityName"])
                            query.equal_to("time", date)
                            query.equal_to("type", t)
                            area_data_list = query.find()
                            if area_data_list == []:
                                ad = area_data()
                                ad.set("event_name", "2019-nCoV")
                                ad.set("area_name", cdata["cityName"])
                                ad.set("time", date)
                                ad.set("type", t)
                            else: 
                                ad = area_data_list[0]
                            ad.set("value", cdata[t])
                            area_data_save_list.append(ad)'''
        '''i = 500
        while i < len(area_save_list):
            area.save_all(area_save_list[i - 500:i])
            i += 500
        area.save_all(area_save_list[i - 500:len(area_save_list)])'''
        i = 500
        while i < len(area_data_save_list):
            area_data.save_all(area_data_save_list[i - 500:i])
            i += 500
        area_data.save_all(area_data_save_list[i - 500:len(area_data_save_list)])